import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Material App',
//      theme: ThemeData(
//          primarySwatch: Colors.lightBlue,
//          scaffoldBackgroundColor: Colors.white),
      home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage>
    with SingleTickerProviderStateMixin {
//1.创建AnimationController
  AnimationController controller;
  Animation animation;
  Animation sizeAnimation;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
//  1.1 初始化AnimationController
    controller = AnimationController(
      vsync: this,
      duration: Duration(milliseconds: 200),
//      lowerBound: 50,
//      upperBound: 150,
    );
//    1.2设置curved值
    animation = CurvedAnimation(parent: controller, curve: Curves.linear);
//    1.3设置tween
    sizeAnimation = Tween(begin: 50.0, end: 150.0).animate(animation);
//    2.监听值的变化
//    controller.addListener(() {
//      setState(() {});
//    });
//    3.监听状态的改变
    controller.addStatusListener((status) {
      if (status == AnimationStatus.completed) {
        controller.reverse();
      } else if (status == AnimationStatus.dismissed) {
        controller.forward();
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    print("_HomePageState1的build方法");
    return Scaffold(
      appBar: AppBar(
        title: Text(
          'Material App Bar',
          style: TextStyle(color: Colors.white),
        ),
      ),
      body: Center(
        child: Container(
          child:IconAnimationWidget(animation:sizeAnimation)// IconAnimationWidget(sizeAnimation),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.play_arrow),
        onPressed: () {
          if (controller.isAnimating) {
            controller.stop();
          } else if (controller.status == AnimationStatus.forward) {
            controller.forward();
          } else if (controller.status == AnimationStatus.reverse) {
            controller.reverse();
          } else {
            controller.forward();
          }
        },
      ),
    );
  }

  @override
  void dispose() {
    controller.dispose();
    // TODO: implement dispose
    super.dispose();
  }
}

class IconAnimationWidget extends AnimatedWidget{
//  final Animation animation;

  IconAnimationWidget({Animation animation}) : super (listenable:animation);

  @override
  Widget build(BuildContext context) {
    Animation animation = listenable;
    return Icon(
      Icons.favorite,
      color: Colors.red,
      size: animation.value,
    );
  }
}

/*
  * 1.Animation:抽象类
  * * 监听动画值的变化
  * * 监听动画状态的变化
  * * value
  * * status
  * 2. AnimationController
  * * vsync：同步信号（this-> with SingleTickerProviderStateMixin）
  * * forward():向前执行动画
  * * reverse():发转执行动画
  * 3.CurvedAnimation
  * * 作用：设置动画执行的速率（速度曲线）
  * 4. Tween: 设置动画执行的value范围
  * *begin：开始值
  * *end: 结束值
  * */
